package cn.kmsoft.spatialboot.config.dbswitch;

import lombok.extern.slf4j.Slf4j;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;

import java.lang.reflect.Method;

/**
 * @author
 * @date 2018/4/19.
 */
@Slf4j
public class DataSourceAdvice implements MethodBeforeAdvice, AfterReturningAdvice {
    @Override
    public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
        DataSourceContextHolder.clearDataSourceType();
    }

    @Override
    public void before(Method method, Object[] objects, Object o) throws Throwable {
//        if (log.isDebugEnabled()) {
//            log.debug("current db instance:{}", objects[0].toString());
//        }
        //包含了传递的参数
        int sizeOfParams=objects.length;
        String dbType=objects[0].toString();
        String dbInstance=objects[1].toString();
        DataSourceContextHolder.setDataSourceType(objects[1].toString().toLowerCase());
    }
}
